import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

import '../../../../utils/httputils/net_api.dart';
import '../../../home/subs/person_detail/models/person_dynamic_model.dart';
import 'index.dart';

class SubscribeController extends GetxController {
  SubscribeController();

  final state = SubscribeState();

  void onRefresh() async {
    state.pageIndex++;
    await postDynamic();
    if (state.rc.isRefresh) {
      state.rc.refreshCompleted(resetFooterState: true);
    }
  }

  void onLoading() {
    if (state.rc.isLoading) {
      state.rc.loadComplete();
    }
  }

  //获取工作动态
  Future<void> postDynamic() async {
    var resultData = await HomeApi.postDynamicList(pageIndex: 0);
    List data = resultData.data['records'];
    state.list = data
        .map(
          (e) => PersonDynamicModel.fromJson(e),
        )
        .toList();
    update();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.rc = RefreshController();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    postDynamic();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.rc.dispose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
